<template>
 <view style="background-color:#f8f8f8;">
  <view class="user-profile-container">
    <view class="header">
      <view class="avatar-container">
        <image class="avatar" :src="avatarSrc" mode="aspectFill"></image> 
      </view>
      <view class="user-info">
        <view style="margin-top:-10px;">
          <text class="username">{{ username }}</text>
        </view>
        <view style="margin-top:5px;">
          <text class="user-id">{{ userPhone }}</text>
        </view>
      </view>
    </view>
  </view>

  <view style="background-color:#f8f8f8;height:600px;">
    <!-- 功能列表部分 -->
    <view class="function-list">
      <view class="function-item" @click="navigateToAddress">
        <image class="icon-image" src="/static/icon/adress.png" />
        <text class="function-text">我的地址</text>
        <image class="icon" src="/static/icon/箭头.png" />
      </view>
      <view class="function-item" @click="navigateToFollow">
        <image class="icon-image" src="/static/icon/我的关注.png" />
        <text class="function-text">我的关注</text>
        <image class="icon" src="/static/icon/箭头.png" />
      </view>
      <view class="function-item" @click="navigateToOrder">
        <image class="icon-image" src="/static/icon/我的订单.png" />
        <text class="function-text">我的订单</text>
        <image class="icon" src="/static/icon/箭头.png" />
      </view>
    </view>
    <!-- 退出登录按钮部分 -->
    <button class="logout-btn" @click="logout">退出登录</button>
  </view>
 </view>
</template>

<script>
	import config from '@/config'
export default {
  data() {
    return {
      avatarSrc: '/static/imgs/assets/header.png',
      username: '小蓝',
      userPhone: 'ID:123456',
      phone: '',
      userInfo: {}
    };
  },
  onLoad() {
    // 从本地存储获取手机号
    this.phone = uni.getStorageSync('currentPhone');
    if (this.phone) {
      this.fetchUserInfoByPhone(this.phone);
    }
  },
  methods: {
    fetchUserInfoByPhone(phone) {
      uni.request({
        url: config.API+`/users?phone=${phone}`,
        success: (res) => {
          if (res.statusCode === 200 && res.data.length > 0) {
            this.userInfo = res.data[0];
            this.username = this.userInfo.username;
            this.userPhone = this.userInfo.phone
           
          }
        },
        
      });
    },
    navigateToAddress() {
      uni.navigateTo({
        url: '/pages/me/address' 
      });
    },
    navigateToFollow() {
      uni.navigateTo({
        url: '/pages/index/more' 
      });
    },
    navigateToOrder() {
      uni.switchTab({
        url: '/pages/order/order' 
      });
    },
    logout() {
      uni.showModal({
        title: '提示',
        content: '确定要退出登录吗？',
        success: (res) => {
          if (res.confirm) {
            uni.removeStorageSync('userInfo');
            uni.removeStorageSync('currentPhone');
            uni.redirectTo({
              url: '/pages/login/login' 
            });
          }
        }
      });
    }
  }
};
</script>

<style scoped>
.user-profile-container {
  background-image: linear-gradient(239deg, #3a6ff3, #50c7fb);
  padding: 20px;
  margin-bottom: 20px; 
  height: 180px;
}

.header {
  display: flex;
  flex-direction: column;
  align-items: center;
  background-color: white;
  border-radius: 10px;
  padding: 20px;
  height: 90px; 
  margin-top: 55px;
}

.avatar-container {
  margin-bottom: 15px;
}

.avatar {
  width: 90px;
  height: 90px;
  border-radius: 50%;
  margin-top: -60px;
}

.user-info {
  text-align: center;
}

.username {
  font-size: 23px;
}

.user-id {
  color: #999;
  font-size: 14px;
}

/* 功能列表样式 */
.function-list {
  background-color: white;
  border-radius: 10px;
  overflow: hidden;
  margin-bottom: 20px; /* 增加底部外边距，使功能列表与退出按钮有间隔 */
  margin-top: 20px;
  margin-left: 23px;
  width: 90%;
  border: 1px solid #f1f1f1;
}

.function-item {
  display: flex;
  align-items: center;
  padding: 23px 20px;
  border-bottom: 1px solid #e0e0e0;
  cursor: pointer;
}

.function-item:last-child {
  border-bottom: none;
}

.icon {
  width: 18px;
  height: 18px;
}

.icon-image {
  margin-right: 15px;
  width: 24px;
  height: 24px;
  background: #32c5ff;
  border-radius: 35%;
  text-align: center;
  font-weight: 700;
  color: #fff;
}

.function-text {
  flex: 1;
  font-size: 16px;
}

.arrow {
  font-family: "Font Awesome 5 Free";
  font-weight: 900;
  color: #999;
}

/* 退出登录按钮样式 */
.logout-btn {
  background-color: #007aff;
  color: white;
  border: none;
  border-radius: 5px;
  padding: 5px;
  width: 90%;
  height: 50px;
  font-size: 16px;
  cursor: pointer;
}
</style>    